#include "events.h"

using namespace bt;

InputListener::InputListener()
{
	keyState = NULL;
}

InputListener::~InputListener()
{
}

// Accessors
bool InputListener::isPressed(const char* name)
{
	if(keyState)
		return keyState[actions[name]];
	else
		return false;
}

// Mutators
void InputListener::addAction(const char* name, Uint8 action)
{
	actions[name] = action;
}

// Actions
void InputListener::update()
{
	keyState = SDL_GetKeyState(NULL);
}

// Inherited from XMLObject
bool InputListener::loadFromXML(xmlDoc* doc, xmlNode* node)
{
	/* Check that root node is an InputListener */
	if(xmlStrcmp(node->name, (xmlChar*) "inputlistener"))
	{
		fprintf(stderr, "InputListener: XML: root node != inputlistener\n");
		return false;
	}
	
	node = node->xmlChildrenNode;
	
	while(node)
	{
		if(!xmlStrcmp(node->name, (xmlChar*) "action"))
		{
			std::string name = XML_getPropString(node, "name");
			
			int action = XML_getPropInt(node, "action");
			
			// Check for invalid action numbers
			if(action < 0)
				action = 0;
			
			// Add the action to the InputListener
			addAction(name.c_str(), action);
		}
		
		node = node->next;
	}
	
	return true;
}
		
void InputListener::writeToXML(xmlDoc* doc, xmlNode* node)
{
}
